package com.example.javaee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.javaee.common.result;
import com.example.javaee.entity.Asset;
import com.example.javaee.entity.AssetOrg;
import com.example.javaee.entity.User;
import com.example.javaee.service.AssetOrgService;
import com.example.javaee.service.AssetService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/assetOrg")
public class AssetOrgController {
    @Autowired
    private AssetOrgService assetOrgService;

    //获取所有安全组织
    @GetMapping("/getAll")
    public result<List<AssetOrg>> getAll(HttpServletRequest request) {
        LambdaQueryWrapper<AssetOrg> queryWrapper = new LambdaQueryWrapper<>();
        // 执行查询
        List<AssetOrg> assetOrgList = assetOrgService.list(queryWrapper);
        return result.success(assetOrgList,"获取成功");

    }

    //获取安全组织的名字
    @GetMapping("/getByid/{id}")
    public result<AssetOrg> getByid(HttpServletRequest request,@PathVariable Integer id) {
        // 执行查询
        AssetOrg assetOrg= assetOrgService.getById(id);
        return result.success(assetOrg,"获取成功");
    }

    //分页获取安全组织
    @GetMapping("/getAssetOrgs/{pg}")
    public result<Page> page(@PathVariable Integer pg){
        //分页构造器
        Page<AssetOrg> pageInfo = new Page<>(pg,12);
        //条件构造器
        LambdaQueryWrapper<AssetOrg> queryWrapper = new LambdaQueryWrapper<>();
        //分页查询
        assetOrgService.page(pageInfo,queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo,"查询成功");

    }
    //更新组织机构
    @PostMapping("/update")
    public result<AssetOrg> update(@RequestBody AssetOrg assetOrg) {
        UpdateWrapper<AssetOrg> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", assetOrg.getId());
        if(assetOrgService.update(assetOrg, updateWrapper)){
            return result.success(null, "更新成功");
        } else {
            return result.error("更新失败");
        }
    }

    //新增安全资产
    @PostMapping("/add")
    public result<AssetOrg> add(HttpServletRequest request, @RequestBody AssetOrg assetOrg) {
        //获取属性
        String orgName = assetOrg.getOrgName();
        String fatherOrg = assetOrg.getFatherOrg();
        //根据页面提交的资产编号orgName查询数据库
        LambdaQueryWrapper<AssetOrg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssetOrg::getOrgName, assetOrg.getOrgName());
        AssetOrg org = assetOrgService.getOne(queryWrapper);
        if (assetOrgService.getOne(queryWrapper) != null) {
            return result.error("资产已存在");
        }
        assetOrgService.save(assetOrg);
        return result.success(null, "资产添加成功");
    }

    //删除组织
    @DeleteMapping("/delete/{id}")
    public result<String> delete(@PathVariable Integer id) {
        if(assetOrgService.removeById(id)){
            return  result.success(null,"删除成功");}
        else{
            return result.error("删除失败");
        }
    }

    //通过所属父类组织搜索
    @GetMapping("/queryByFatherOrg/{input}/{pg}")
    public result<Page> fatherOrgPage(@PathVariable Integer pg, @PathVariable String input) {
        // 分页构造器
        Page<AssetOrg> pageInfo = new Page<>(pg, 12);
        // 条件构造器
        LambdaQueryWrapper<AssetOrg> queryWrapper = new LambdaQueryWrapper<>();
        if (input != null && !input.isEmpty()) {
//            模糊搜索
            queryWrapper.like(AssetOrg::getFatherOrg, "%" + input + "%");
        }
        else {
            return result.error("请输入搜索内容");
        }
        // 分页查询
        assetOrgService.page(pageInfo, queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo, "查询成功");
    }


//通过组织名字查询
    @GetMapping("/queryByOrgName/{input}/{pg}")
    public result<Page> orgNamePage(@PathVariable Integer pg, @PathVariable String input) {
        // 分页构造器
        Page<AssetOrg> pageInfo = new Page<>(pg, 12);
        // 条件构造器
        LambdaQueryWrapper<AssetOrg> queryWrapper = new LambdaQueryWrapper<>();
        if (input != null && !input.isEmpty()) {
//            模糊搜索
            queryWrapper.like(AssetOrg::getOrgName, "%" + input + "%");
        }
        else {
            return result.error("请输入搜索内容");
        }
        // 分页查询
        assetOrgService.page(pageInfo, queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo, "查询成功");
    }

}
